package flc.ast.utils;

import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;

import com.blankj.utilcode.util.FileUtils;
import com.blankj.utilcode.util.ImageUtils;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class MyUtils {

    //获取应用大小
    public static long getAppSize(Context context, String packageName) {
        long size = 0;
        try {
            // 获取PackageManager实例
            PackageManager pm = context.getPackageManager();
            // 获取包信息
            PackageInfo packageInfo = pm.getPackageInfo(packageName, 0);
            // 应用代码大小
            File apkFile = new File(packageInfo.applicationInfo.sourceDir);
            size = apkFile.length();
        } catch (PackageManager.NameNotFoundException | IllegalStateException e) {
            e.printStackTrace();
        }
        return size;
    }

    //获取安装日期
    public static long getInstallDate(Context context, String packageName) {
        PackageManager packageManager = context.getPackageManager();
        long installDate = 0;
        try {
            PackageInfo packageInfo = packageManager.getPackageInfo(packageName, 0);
            installDate = packageInfo.firstInstallTime;
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
        return installDate;
    }

    //获取文件size
    public static long getFileSize(File file) {
        long size = 0;
        if (file.exists()) {
            FileInputStream fis = null;
            try {
                fis = new FileInputStream(file);
                size = fis.available();
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return size;
    }

    //获取对应集合
    public static List<String> getRecordList(int kind, List<String> list) {
        List<String> listResult = new ArrayList<>();
        List<String> listPicture = new ArrayList<>();
        List<String> listVideo = new ArrayList<>();
        List<String> listMusic = new ArrayList<>();
        List<String> listApk = new ArrayList<>();
        if (list != null) {
            for (String path : list) {
                if (ImageUtils.isImage(path)) {
                    listPicture.add(path);
                } else if (FileUtils.getFileExtension(path).equals("mp3")) {
                    listMusic.add(path);
                } else if (FileUtils.getFileExtension(path).equalsIgnoreCase("mp4")
                        || FileUtils.getFileExtension(path).equalsIgnoreCase("mkv")
                        || FileUtils.getFileExtension(path).equalsIgnoreCase("3gp")
                        || FileUtils.getFileExtension(path).equalsIgnoreCase("mov")
                        || FileUtils.getFileExtension(path).equalsIgnoreCase("avi")) {
                    listVideo.add(path);
                } else {
                    listApk.add(path);
                }
            }
        }
        switch (kind) {
            case 1:
                listResult.addAll(listPicture);
                break;
            case 2:
                listResult.addAll(listVideo);
                break;
            case 3:
                listResult.addAll(listMusic);
                break;
            case 4:
                listResult.addAll(listApk);
                break;
        }
        return listResult;
    }
}
